home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / LList Mgr / LList Mgr For Think C / Sample LList.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-22  |  6.2 KB  |  240 lines  |  [TEXT/KAHL]

  1. /************************************************************/
  2. /*                                                            */
  3. /* Sample LList.c                                            */
  4. /*                                                            */
  5. /* The following code demonstrates the use of an LList in     */
  6. /* a modal dialog. See LList.h for documentation on each    */
  7. /* LList function.                                            */
  8. /*                                                            */
  9. /************************************************************/
  10.  
  11. #include "LList.h"
  12.  
  13. #define DIALOG_RSRC_ID 128
  14. #define DIALOG_LIST_ITEM 3
  15. #define DIALOG_SORT_FWD_ITEM 4
  16. #define DIALOG_SORT_BKWD_ITEM 5
  17. #define DIALOG_OK_ITEM 1
  18. #define DIALOG_CANCEL_ITEM 2
  19.  
  20. /************************************************************/
  21.  
  22. pascal Boolean myDialogFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  23. {
  24.     GrafPtr        remPort;
  25.     Point        thePt;
  26.     Rect        theRect;
  27.     char        theChar;
  28.     short        doubleClick;
  29.     
  30.     switch (theEvent->what)
  31.     {
  32.         case keyDown:
  33.             // if key pressed, handle return key
  34.             theChar = (theEvent->message) & charCodeMask;
  35.             if ((theChar == 0x0D) || (theChar == 0x03))
  36.             {
  37.                 *itemHit = ((DialogPeek)theDialog)->aDefItem;
  38.                 return TRUE;
  39.             }
  40.             return FALSE;
  41.             
  42.         case mouseDown:
  43.             // get where the click occured in global coords
  44.             thePt = theEvent->where;
  45.             
  46.             // save the current port
  47.             GetPort(&remPort);
  48.             SetPort(theDialog);
  49.             
  50.             // convert click to local coords
  51.             GlobalToLocal(&thePt);
  52.             
  53.             // get a copy of the list's view rectangle
  54.             theRect.top = ((LList *)(((WindowPeek)theDialog)->refCon))->view.top;
  55.             theRect.bottom = ((LList *)(((WindowPeek)theDialog)->refCon))->view.bottom;
  56.             theRect.left = ((LList *)(((WindowPeek)theDialog)->refCon))->view.left;
  57.             theRect.right = ((LList *)(((WindowPeek)theDialog)->refCon))->view.right + 16;
  58.             
  59.             if (!PtInRect(thePt, &theRect))
  60.             {
  61.                 // mouse was not clicked in list
  62.                 SetPort(remPort);
  63.                 return FALSE;
  64.             }
  65.             
  66.             // mouse was clicked in list
  67.             *itemHit = DIALOG_LIST_ITEM;
  68.             doubleClick = LLClick((LList *)(((WindowPeek)theDialog)->refCon), thePt, theEvent->modifiers);
  69.             
  70.             // automatically push OK if user double-clicks
  71.             if (doubleClick)
  72.                 *itemHit = ((DialogPeek)theDialog)->aDefItem;
  73.             else 
  74.                 *itemHit = DIALOG_LIST_ITEM;
  75.                 
  76.             // restore port and let ModalDialog know we handled event
  77.             SetPort(remPort);
  78.             return TRUE;
  79.             
  80.         case updateEvt:
  81.             BeginUpdate(theDialog);
  82.             LLUpdate((LList *)(((WindowPeek)theDialog)->refCon));
  83.             DrawDialog(theDialog);
  84.             EndUpdate(theDialog);
  85.             return FALSE;
  86.  
  87.         default:
  88.             return FALSE;
  89.     }
  90. } // myDialogFilter
  91.  
  92. /************************************************************/
  93.  
  94. void DoSampleLList(Str255 city, Str255 state, Str255 someNumber)
  95. {
  96.     DialogPtr    theDialog;
  97.     short        itemHit;
  98.     short        iType;
  99.     Handle        iHandle;
  100.     Rect        iRect;
  101.     LList        *theList;
  102.     LRow        *row;
  103.     short        dataLen;
  104.     
  105.     // make city,state,someNumber initially empty;
  106.     // if the user selects a row and presses OK, 
  107.     // they will contain data from the selected row
  108.     city[0] = '\0';
  109.     state[0] = '\0';
  110.     someNumber[0] = '\0';
  111.     
  112.     // get the sample dialog
  113.     theDialog = GetNewDialog(DIALOG_RSRC_ID, NULL, (WindowPtr)-1);
  114.  
  115.     // get Rect of list dialog item
  116.     GetDItem(theDialog, DIALOG_LIST_ITEM, &iType, &iHandle, &iRect);
  117.     
  118.     // create a list in the dialog 16 pixels high with 3 columns
  119.     theList = LLNew(&iRect, theDialog, 16, 3, true, LLOnlyOne);
  120.     
  121.     theList->colDesc[1].justify = 1;    // center column 2
  122.     theList->colDesc[2].justify = -1;    // right justify column 3
  123.  
  124.     // remember theList for myDialogFilter
  125.     ((WindowPeek)theDialog)->refCon = (long)theList;
  126.  
  127.     // turn off list drawing while we add rows of data to the list
  128.     LLDoDraw(theList, 0, 0);
  129.     
  130.     // add some rows with data to the list
  131.     row = LLAddRow(theList, NULL);
  132.     LLSetCell(theList, row, 0, 7, "Georgia");
  133.     LLSetCell(theList, row, 1, 7, "Atlanta");
  134.     LLSetCell(theList, row, 2, 1, "1");
  135.     
  136.     row = LLAddRow(theList, NULL);
  137.     LLSetCell(theList, row, 0, 8, "Virginia");
  138.     LLSetCell(theList, row, 1, 7, "Roanoke");
  139.     LLSetCell(theList, row, 2, 2, "22");
  140.     
  141.     row = LLAddRow(theList, NULL);
  142.     LLSetCell(theList, row, 0, 8, "Virginia");
  143.     LLSetCell(theList, row, 1, 7, "Norfolk");
  144.     LLSetCell(theList, row, 2, 3, "333");
  145.  
  146.     row = LLAddRow(theList, NULL);
  147.     LLSetCell(theList, row, 0, 7, "Indiana");
  148.     LLSetCell(theList, row, 1, 9, "Ft. Wayne");
  149.     LLSetCell(theList, row, 2, 4, "4444");
  150.  
  151.     // turn list drawing back on
  152.     LLDoDraw(theList, 1, 0);
  153.     
  154.     // initially disable OK button
  155.     GetDItem(theDialog, DIALOG_OK_ITEM, &iType, &iHandle, &iRect);
  156.     HiliteControl((ControlHandle)iHandle, 255);
  157.     
  158.     do {
  159.         ModalDialog(myDialogFilter, &itemHit);
  160.         
  161.         switch (itemHit)
  162.         {
  163.             case DIALOG_SORT_FWD_ITEM:
  164.                 // sort the list by State, City, case-insensitive, in-order
  165.                 LLSort(theList, 0, 1, -1, 1, 1);
  166.                 break;
  167.                 
  168.             case DIALOG_SORT_BKWD_ITEM:
  169.                 // sort the list by State, City, case-insensitive, reverse-order
  170.                 LLSort(theList, 0, 1, -1, 1, -1);
  171.                 break;
  172.         
  173.             default:
  174.                 // dim OK button if no row is selected
  175.                 row = LLNextRow(theList, NULL);
  176.                 if (!LLGetSelect(theList, &row, 1))
  177.                 {
  178.                     HiliteControl((ControlHandle)iHandle, 255);
  179.                     if (itemHit == DIALOG_OK_ITEM)
  180.                         itemHit = 0; // in case user hit return
  181.                 }
  182.                 else
  183.                     HiliteControl((ControlHandle)iHandle, 0);
  184.                 break;
  185.         }
  186.         
  187.     } while ((itemHit != DIALOG_OK_ITEM) && (itemHit != DIALOG_CANCEL_ITEM));
  188.  
  189.     // if user presses ok get the city name from the selected row
  190.     if (itemHit == DIALOG_OK_ITEM)
  191.     {
  192.         // get pointer to first row
  193.         row = LLNextRow(theList, NULL);
  194.         
  195.         // find first selected row; start looking in first row
  196.         if (LLGetSelect(theList, &row, 1))
  197.         {
  198.             // get selected state
  199.             dataLen = 255;
  200.             LLGetCell(theList, row, 0, &dataLen, (Ptr)&(state[1]));
  201.             state[0] = (char)dataLen;
  202.             
  203.             // get selected city
  204.             dataLen = 255;
  205.             LLGetCell(theList, row, 1, &dataLen, (Ptr)&(city[1]));
  206.             city[0] = (char)dataLen;
  207.  
  208.             // get selected someNumber
  209.             dataLen = 255;
  210.             LLGetCell(theList, row, 2, &dataLen, (Ptr)&(someNumber[1]));
  211.             someNumber[0] = (char)dataLen;
  212.         }
  213.     }
  214.     
  215.     // dispose of list
  216.     LLDispose(theList);
  217.     
  218.     // dispose of dialog
  219.     DisposDialog(theDialog);
  220. } // DoSampleLList
  221.  
  222. /************************************************************/
  223.  
  224. main()
  225. {
  226.     Str255        city;
  227.     Str255        state;
  228.     Str255        someNumber;
  229.  
  230.     InitGraf(&thePort);
  231.     InitFonts();
  232.     InitWindows();
  233.     InitDialogs(0L);
  234.     FlushEvents(everyEvent, 0);
  235.     InitCursor();
  236.     DoSampleLList(city, state, someNumber);
  237. } // main
  238.  
  239. /************************************************************/
  240.